package com.tanhua.server.controller;

import com.tanhua.domain.db.Settings;
import com.tanhua.domain.db.User;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.SettingsService;
import com.tanhua.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("users")
public class SettingsController {
    @Autowired
    private SettingsService settingsService;
    @Autowired
    private UserService userService;

    /**
     * 接口名称：用户通用设置 - 读取
     * 接口路径：GET/users/settings
     * 需求分析：根据用户id查询通用设置
     */
    @GetMapping("settings")
    public ResponseEntity<Object> querySettings() {
        return settingsService.querySettings();
    }

    /**
     * 接口名称：通知设置 - 保存
     * 接口路径：POST/users/notifications/setting
     * 需求：添加或者修改通用设置  tb_settings
     */
    @PostMapping("notifications/setting")
    public ResponseEntity<Object> saveNotification(@RequestBody Settings settings) {
        return settingsService.saveNotification(settings);
    }

    /**
     * 接口名称：设置陌生人问题 - 保存
     * 接口路径：POST/users/questions
     * 需求描述：添加或者修改陌生人问题
     */
    @PostMapping("questions")
    public ResponseEntity<Object> saveQuestion(
            @RequestBody Map<String, String> param) {
        // 获取请求参数: 陌生人问题
        String content = param.get("content");
        return settingsService.saveQuestion(content);
    }

    /**
     * 接口名称：黑名单 - 翻页列表
     * 接口路径：GET/users/blacklist
     *
     * @RequestParam
     * 1. 当请求参数与方法形参名称不一致可以使用它建立映射关系
     * 2. 通过此注解可以给参数设置默认值
     */
    @GetMapping("blacklist")
    public ResponseEntity<Object> queryBlacklist(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize) {
        return settingsService.queryBlacklist(page,pagesize);
    }

    /**
     * 接口名称：黑名单 - 移除
     * 接口路径：DELETE/users/blacklist/50
     *
     * @PathVariable 获取rest请求路径中占位符值
     */
    @DeleteMapping("blacklist/{uid}")
    public ResponseEntity<Object> deleteBlackUser(
            @PathVariable("uid") Long blackUserId) {
        return settingsService.deleteBlackUser(blackUserId);
    }

    /**
     * 接口名称：修改手机号 - 1 发送验证码
     * 接口路径：POST/users/phone
     */
    @PostMapping("phone/sendVerificationCode")
    public ResponseEntity<Object> sendVerificationCode(){
        User user = UserHolder.get();
        return userService.login(user.getMobile());
    }

    /**
     * 接口名称：修改手机号 - 2 校验验证码
     * 接口路径：POST/users/phone
     */
    @PostMapping("phone/checkVerificationCode")
    public ResponseEntity<Object> checkVerificationCode(@RequestBody Map<String, String> map){
        String verificationCode = map.get("verificationCode");
        User user = UserHolder.get();
        return userService.loginVerification(user.getMobile(), String.valueOf(verificationCode));
    }

    /**
     * 接口名称：修改手机号 - 3 保存
     * 接口路径：POST/users/phone
     */
    @PostMapping("phone")
    public ResponseEntity<Object> savePhone(@RequestBody Map<String, String> map){
        String phone = map.get("phone");
        return userService.savePhone(phone);
    }
}
